package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Counter;
import beans.UserDAO;

/**
 * Servlet implementation class InitServlet
 */

//本程序是由第一个页面index.jsp导向，主要作用是进行一些初始设置
//如设置session的有效时间，如根据侦听到的会话来计算网站访问人数，计算访问人数用了3种方法，但数据库和文件方案准确
@WebServlet("/InitServlet")
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		
        HttpSession session=request.getSession(true);
        session.setMaxInactiveInterval(10*60);//10*60秒，即10分钟 。session 是生效了，但是main.jsp只有主动刷新，或点其他功能项后，才会跳到登录login.jsp页面
		boolean newflag=session.isNew();//此句无用了，看下句有用。因为使用了侦听器，也以到这里就算是老会话了
		newflag=(boolean)request.getServletContext().getAttribute("newsession");//此句使用侦听器里 听到了作为判断是否是新的标记	


		//方案1  网站访问人数
		Object obj=request.getServletContext().getAttribute("counter");//request.getServletContext()相当于application
		if(obj==null){
			Counter counter=new Counter();
			request.getServletContext().setAttribute("counter", counter);
		}//此方法不能防止刷新和tomcat重新启动
		
		//方案2 好，网站访问人数保存成数据库
		UserDAO userDAO2=new UserDAO();
		int current2;
		
		if(newflag){
			//out.print("new session db");
			System.out.println("new session db");
			current2=userDAO2.addnum();//1表示要增加		
		}else{
			//out.print("old session db");
			System.out.println("old session db");
			current2=userDAO2.getnum();//2只是取数据
		}
		request.getServletContext().setAttribute("current2", current2);
		
		//方案3好，网站访问人数保存成文件
		UserDAO userDAO3=new UserDAO();
		int current3;
		if(newflag){
			//out.print("new SESSION FILE");
			System.out.println("new SESSION FILE");
			current3=userDAO3.addnumfile(request);//1表示要增加		
		}else{
			//out.print("old SESSION FILE");
			System.out.println("old SESSION FILe");
			current3=userDAO3.getnumfile(request);//2只是取数据
		}
		request.getServletContext().setAttribute("current3", current3);
		
		
		//最后，跳到登录页面login.jsp
		response.sendRedirect("login.jsp");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
